<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">发送</button>
    <script>
      // XMLHttpRequest 发送异步请求

      document.querySelector("#btn").onclick = function () {
        // 创建 XMLHttpRequest 对象
        let request = new XMLHttpRequest();

        request.open("post", "http://127.0.0.1:8080/login", true);

        // 设置请求头
        request.setRequestHeader(
          "Content-Type",
          "application/x-www-form-urlencoded"
        );

        // 发送请求
        request.send("captcha=1234&username=123&password=123");

        // 监听请求状态
        request.onreadystatechange = function () {
          if (request.readyState === 4 && request.status === 200) {
            // 请求成功
            // 响应内容
            console.log(request.responseText);
          }
        };

        console.log("请求已发送");
      };
    </script>
  </body>
</html>
